<?php
class SelectedprofessionsController extends AppController {

	var $name = 'Selectedprofessions';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Selectedprofession->recursive = 0;
		$this->set('selectedprofessions', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Selectedprofession', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('selectedprofession', $this->Selectedprofession->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Selectedprofession->create();
			if ($this->Selectedprofession->save($this->data)) {
				$this->Session->setFlash(__('The Selectedprofession has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Selectedprofession could not be saved. Please, try again.', true));
			}
		}
		if ($this->params['named']['abiturient']){
			$this->Selectedprofession->Abiturient->id = $this->params['named']['abiturient'];
		}
		else {
			$this->Selectedprofession->Abiturient->id = $this->data['Selectedprofession']['abiturient_id'];
		}		
		$abiturient = $this->Selectedprofession->Abiturient->read(array('Abiturient.lastname', 'Abiturient.firstname', 'Abiturient.surname', 'Abiturient.id'));
		$facultets = $this->Selectedprofession->Profession->Facultet->find('list');
		//	$professions = $this->Selectedprofession->Profession->find('list');
		$learningforms = $this->Selectedprofession->Learningform->find('list');
		$learningtypes = $this->Selectedprofession->Learningtype->find('list');
		//, 'professions'
		$this->set(compact('abiturient', 'learningtypes', 'learningforms', 'facultets'));
	}

/**
 * @author Adm
 *	для ajax обновляет select с профессиями по выбранному факультету
 */	
	function update_profession() {
	  if(!empty($this->data['Selectedprofession']['facultet_id'])) {
	    $facultet_id = $this->data['Selectedprofession']['facultet_id'];
	    $options = $this->Selectedprofession->Profession->find('list', array('conditions' => array ('Profession.facultet_id' => $facultet_id)));
	    $this->set(compact('options'));
	  }
	}	
	
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Selectedprofession', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Selectedprofession->save($this->data)) {
				$this->Session->setFlash(__('The Selectedprofession has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Selectedprofession could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Selectedprofession->read(null, $id);
		}
		$abiturients = $this->Selectedprofession->Abiturient->find('list');
		$professions = $this->Selectedprofession->Profession->find('list');
		$learningforms = $this->Selectedprofession->Learningform->find('list');
		$learningtypes = $this->Selectedprofession->Learningtype->find('list');
		$this->set(compact('abiturients','professions','learningforms','learningtypes'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Selectedprofession', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->Selectedprofession->del($id)) {
			$this->Session->setFlash(__('Selectedprofession deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Selectedprofession could not be deleted. Please, try again.', true));
		$this->redirect(array('action' => 'index'));
	}

}
?>